NVCC=nvcc
CC=gcc
LINK=g++

INCLUDES = -I. -I../libs -I/usr/local/cuda/include
LIBS = -L/usr/local/cuda/lib -lcuda -lcudart
GL_LIBS = -lGLEW

# detect OS
OSUPPER = $(shell uname -s 2>/dev/null | tr [:lower:] [:upper:])
# 'linux' is output for Linux system, 'darwin' for OS X
DARWIN = $(strip $(findstring DARWIN, $(OSUPPER)))
ifneq ($(DARWIN),)
    INCLUDES += -I/opt/local/include
    LIBS += -L/opt/local/lib
    GL_LIBS += -framework glut -framework OpenGL 
    CUDA_SDK=/Developer/GPU\ Computing/C
else
    INCLUDES += -I/usr/include -I/usr/local/include
    LIBS += -L/usr/lib -L/usr/local/lib
    GL_LIBS += -L/usr/X11/lib -lglut -lGL -lGLU
    CUDA_SDK=$(HOME)/NVIDIA_GPU_Computing_SDK/C
endif

# detect OS
ARCHUPPER = $(shell uname -m 2>/dev/null | tr [:lower:] [:upper:])
# 'linux' is output for Linux system, 'darwin' for OS X
ARCH := $(strip $(findstring X86_64, $(ARCHUPPER)))
ifneq ($(ARCH),)
    LIB_ARCH = x86_64
    NVCCFLAGS += -m64
    ifneq ($(DARWIN),)
        CXX_ARCH_FLAGS += -arch x86_64
    else
        CXX_ARCH_FLAGS += -m64
    endif
else
    LIB_ARCH = i386
    NVCCFLAGS += -m32
    ifneq ($(DARWIN),)
        CXX_ARCH_FLAGS += -arch i386
    else
        CXX_ARCH_FLAGS += -m32
    endif
endif


INCLUDES += -I$(CUDA_SDK)/common/inc
LIBS += -L$(CUDA_SDK)/lib -lcutil_$(LIB_ARCH)

LIBS += -L../libs -licgip_common -licgip_opengl -ldpc_cuda

BINARY=main

$(BINARY): $(BINARY).o thresholding.o
	$(LINK) $(CXX_ARCH_FLAGS) $(LIBS) $(GL_LIBS) $^ -o $(BINARY)

$(BINARY).o: $(BINARY).c
	$(CC) $(CXX_ARCH_FLAGS) -c $(INCLUDES) $< -o $@

thresholding.o: thresholding.cu
	$(NVCC) $(NVCCFLAGS) -c $(INCLUDES) $< -o $@
clean:
	rm -f *.o
	rm -f $(BINARY)
	rm -f lena.raw
	rm -f lena_filtered.raw
	rm -f lena_filtered.png

lena.raw: data/lena.png
	convert data/lena.png gray:lena.raw

lena_filtered.raw: lena.raw $(BINARY)
	./main lena.raw 512 512 lena_filtered.raw 128

lena_filtered.png: lena_filtered.raw
	convert -size 512x512 -depth 8 gray:lena_filtered.raw lena_filtered.png

.PHONY: test clean

test: lena_filtered.png
